<?php
/**
 * 红包列表、红包详情、领取红包记录
 * Class MeRed
 */
namespace App;
defined('IN_IA') or exit('Access Denied');
use Model\MediaVideoMod;
use Model\RedMod;
use Model\RedQuestionMod;
use Model\RedRecordMod;
use Pub\cache\Cache;

class RedDetail extends Action
{
    private $Red = null;
    private $RedRecord = null;
    private $MediaVideo = null;
    private $RedQuestion = null;

    private $_red_id = 0;  //红包id
    private $_red_type = 'normal';  //红包的类型

    public function __construct()
    {
        parent::__construct();
        $this->Red = new RedMod();
        $this->getUid();
        if($this->_gpc['red_id']) $this->_red_id = $this->_gpc['red_id'];
        if($this->_gpc['red_type']) $this->_red_type = $this->_gpc['red_type'];
    }

    /**
     * 开红包时所需红包相关内容
     * @param
     */
    public function openRedDetail()
    {
        $_detail = $this->getRedPackage($this->_red_id);
        if($_detail){
            $_detail['status'] = $this->getRedStatus($_detail);
            $_member = $this->uidToInfo($_detail['uid']);
            $_detail['nickname'] = $_member['nickname'];
            $_detail['header'] = $_member['avatar'];
            if($_detail['media']) $_detail['src'] = array(tomedia($_detail['media']));
            if($this->_red_type === "video"){
                $_video = $this->redMediaToVideo($_detail['media']);
                $_detail["video_thumb"] = $_video["thumb"];
                unset($this->MediaVideo);
            }
            $this->_result = array('status'=>'success','data'=>$_detail);
        }
        $this->result($this->_result);
    }

    /**
     * 返回红包状态
     * @param array $_detail 红包详情
     * @return array
     */
     private function getRedStatus($_detail)
     {
         $_status = array('status'=>$_detail['status']);
         $this->RedRecord = new RedRecordMod();
         if($_record = $this->RedRecord->findOne(array('red_id'=>$this->_red_id,'uid'=>$this->_uid),array('money'))){
            $_status = array('status'=>5,'money'=>$_record['money']);   //已经抢过该红包
         }else{
             if(($_detail['already_number'] >= $_detail['number']) && $_detail['status'] == 2){
                 $this->Red->update(array('status'=>3),array('id'=>$this->_red_id));
                 $_status = array('status'=>3);  //红包已经开完
             }
         }
        return $_status;
     }

    /**
     * 获取指定用户的红包列表
     * @param array 请求数量下标
     * @param Number enter_red_id app端入口红包id 过滤列表中入口红包
     * @param array area 用户所在地区
     */
    public function getList()
    {
        $_list = $this->Red->openRedList($this->_uid,$this->_gpc['enter_red_id'],$this->_uniacid,$this->_gpc['limit']);
        if(count($_list) > 0){
            $_data = array();
            foreach($_list as $_k=>$_v){
                if($_v["area_type"] !== '0'){
                    $_area = json_decode($_v["area"],true);
                    $_index = $_v["area_type"]-1;
                    if($this->_gpc["area"][$_index] != $_area[$_index]){
                        continue;
                    }
                }
                $_data[] = array("id"=>$_v["id"],"red_type"=>$_v["red_type"]);
            }
            $this->_result = array('status'=>'success','data'=>$_data);
            unset($_list);
        }
        $this->result($this->_result);
    }

    /**
     * 获取指定用户的红包总数
     * @param string uid 用户id
     */
    public function getUserRedTotal()
    {
        $this->_result =
                array(
                    'status'=>'success',
                    'data'=>$this->Red->total(array('uid'=>$this->_uid,'uniacid'=>$this->_uniacid,'status <'=>9,'status !='=>1))
            );
        $this->result($this->_result);
    }

    /**
     * 获取红包指定详情
     * @param integer red_id 红包id
     */
    public function easyFind()
    {
        $_return = array();
        if(isset($this->_gpc['return'])) $_return = $this->_gpc['return'];
        $_red = $this->Red->findOne(array('id'=>$this->_gpc['red_id']),$_return);
        if($_red) $this->_result = array('status'=>'success','data'=>$_red);
        $this->result($this->_result);
    }

    /**
     * 红包答题信息,cache
     * @param int red_id 红包id
     */
    public function redQuestion()
    {
        $_question = Cache::get(Cache::RED_QUESTION,array("red_id"=>$this->_gpc["red_id"]));
        if(!$_question || KUYUAN_DEBUG){
            $this->RedQuestion = new RedQuestionMod();
            $_question = $this->RedQuestion->findOne(array("red_id"=>$this->_gpc["red_id"]),array("question","options","answer"));
            $_options = json_decode($_question["options"],true);
            if($_options && count($_options) > 0){
                $_new_options = array();
                foreach($_options as $_v){
                    if(!empty($_v)){
                        $_new_options[] = $_v;
                    }
                }
                unset($_options);
                $_question["options"] = $_new_options;
            }
            Cache::set(Cache::RED_QUESTION,$_question,array("red_id"=>$this->_gpc["red_id"]));
        }
        $this->_result = array("status"=>"success","data"=>$_question);
        $this->result($this->_result);
    }


    //----------web端红包列表----------------
    /**
     * 红包总数
     * @param string red_type 红包类型
     */
    public function getTotal()
    {
        $_where = array("uniacid"=>$this->_uniacid,"status >"=>"1","status <"=>"9","red_type"=>$this->_gpc["red_type"]);
        $this->_result = array("status"=>"success","data"=>$this->Red->total($_where));
        $this->result($this->_result);
    }

    /**
     * 红包列表
     * @param string red_type 红包类型
     * @param array limit 请求下标
     */
    public function getWebList()
    {
        $_where = array("uniacid"=>$this->_uniacid,"status >"=>"1","status <"=>"9","red_type"=>$this->_gpc["red_type"]);
        $_list = $this->Red->findAll(
            $_where,
            array("order"=>array("status","asc","id","desc"),"limit"=>$this->_gpc["limit"]),
            array("id","uid","type","red_type","title","media","money","number")
        );
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_member = $this->uidToInfo($_v["uid"]);
                $_list[$_k]["nickname"] = $_member["nickname"];
                if($_v["type"] == 2) $_list[$_k]["money"] = sprintf("%.2f",$_v["money"] * $_v["number"]);
                $_str_type = $_v["type"] == 2 ? "普通" : "拼手气";
                $_list[$_k]["type"] = $this->redType($_v["red_type"]) .' / '.$_str_type;
                if($_v["red_type"] == "pic"){
                    $_list[$_k]["media"] = tomedia($_v["media"]);
                }else if($_v["red_type"] == "video"){
                    $_video = $this->redMediaToVideo($_v["media"]);
                    $_list[$_k]["video_thumb"] = $_video["thumb"];
                    $_list[$_k]["video"] = $_video["video"];
                }else{
                    unset($_list[$_k]["media"]);
                }
                unset($_list[$_k]["uid"],$_list[$_k]["red_type"]);
            }
            unset($_member);
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 红包类型
     * @param string $_type 红包类型
     * @return string 红包类型
     */
    private function redType($_type)
    {
        $_str_type = "";
        switch($_type){
            case "normal":
                $_str_type = "默认";
                break;
            case "pic":
                $_str_type = "图片";
                break;
            case "video":
                $_str_type = "视频";
                break;
        }
        return $_str_type;
    }

    /**
     * 通过红包media获取视频红包的封面
     * @param $_media
     * @return mixed
     */
    private function redMediaToVideo($_media)
    {
        if(!$this->MediaVideo instanceof MediaVideoMod) $this->MediaVideo = new MediaVideoMod();
        $_media_video = $this->MediaVideo->findOne(array("source"=>$_media),array("thumb","source"));
        return array("thumb"=>tomedia($_media_video["thumb"]),"video"=>tomedia($_media_video["source"]));
    }

}
